package com.jxt.myapp.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class WebUtils {
	
	
	/**
	 * 创建一个下拉列表
	 * @param list
	 * @param keyName
	 * @param valueName
	 * @param other
	 * @param space
	 * @param setEmpty
	 * @return
	 */
	public static String buildSelect(List<Map<String, Object>> list,
			String keyName, String valueName, String[] other, String space,
			boolean setEmpty) {
		StringBuilder options = new StringBuilder();
		boolean addSpace = false;
		if (setEmpty) {
			options.append("<option value=\"\" selected>" + "" + space
					+ "</option>");
			addSpace = true;
		}
		if (other != null) {
			for (int i = 0; i < other.length; i++) {
				String oneOther = other[i];
				if (addSpace) {
					options.append("<option value=\"" + oneOther + "\">"
							+ oneOther + "</option>");
				} else {
					options.append("<option value=\"" + oneOther
							+ "\" selected>" + oneOther + space + "</option>");
					addSpace = true;
				}
			}
		}

		Iterator<Map<String, Object>> ite = list.iterator();
		while (ite.hasNext()) {
			Map map = ite.next();
			String value = (String) map.get(keyName);
			String disp = (String) map.get(valueName);
			if (addSpace) {
				options.append("<option value=\"" + value + "\">" + disp
						+ "</option>");
			} else {
				options.append("<option value=\"" + value + "\" selected>"
						+ disp + space + "</option>");
				addSpace = true;
			}
		}
		return options.toString();

	}

}
